﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Reflection;

namespace MillerApp.CommonLib.Utility
{
    /// <summary>
    /// 集合服务类
    /// </summary>
    public class CollectionHelper
    {
        public CollectionHelper()
        {
        }

        /// <summary>
        /// 创建一个以T所有属性为列的DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public DataTable CreateTable<T>()
        {
            Type componentType = typeof(T);
            DataTable table = new DataTable(componentType.Name);
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(componentType))
            {
                table.Columns.Add(descriptor.Name, descriptor.PropertyType);
            }
            return table;
        }

        /// <summary>
        /// 从一个对象集合创建一个DataTable（以T所有属性为列的DataTable）
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="list">对象集</param>
        /// <returns>数据表</returns>
        public DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type componentType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(componentType);
            foreach (T local in list)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor descriptor in properties)
                {
                    row[descriptor.Name] = descriptor.GetValue(local);
                }
                table.Rows.Add(row);
            }
            return table;
        }

        public List<T> ConvertToList<T>(DataTable dt, IDictionary<string, string> columnPropertyMapping)
        {
            IDictionary<int, string> columnPropertyMap = new Dictionary<int, string>();
            ReflectionHelper refHelper = new ReflectionHelper();
            int columnCount = dt.Columns.Count;

            List<T> list = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                //构建对象
                T localItem = default(T);
                T item = (localItem == null) ? Activator.CreateInstance<T>() : default(T);
                //构建对象数据
                for (int i = 0; i < columnCount; i++)
                {
                    string propertyName = dt.Columns[i].ColumnName;
                    if (columnPropertyMap.ContainsKey(i))
                    {
                        propertyName = columnPropertyMap[i];
                    }
                    else if (columnPropertyMapping.ContainsKey(propertyName))
                    {
                        propertyName = columnPropertyMapping[propertyName];
                        columnPropertyMap.Add(new KeyValuePair<int, string>(i, propertyName));
                    }
                    object propertyValue = dr[i];
                    refHelper.SetPropertyValue(item, propertyName, propertyValue);
                }
                list.Add(item);
            }
            return list;
        }

        public static string ConvertListToSQLString<T>(IList<T> list)
        {
            return string.Empty;
        }
    }
}
